<?php

//GET  /volumes?q={search terms}
//GET https://www.googleapis.com/books/v1/volumes?q=the+liar&key={YOUR_API_KEY}

include 'functions.php';
require 'secure.php';

echo '<form name="title" method="post" autocomplete="on">';
echo '<input type=text name="title" required/>';
echo '<br/><input type="submit" value="Submit"/>';
echo '<input type=hidden name="submitted" value="1"/>';
echo '</form>';

if ($_POST[submitted]==1)
{

	//starting information
	$rootTitle = $_POST[title];
	$rootTitle = stripslashes($rootTitle);

	//parse determiners from title and put them back at the front
	//The
	if (stripos($rootTitle, ', the')>=1)
	{
		$rootTitle = str_ireplace(', the', '', $rootTitle);
		$rootTitle = 'the '.$rootTitle;
	}
	//An
	if (stripos($rootTitle, ', an')>=1)
	{
		$rootTitle = str_ireplace(', an', '', $rootTitle);
		$rootTitle = 'an '.$rootTitle;
	}
	//A
	if (stripos($rootTitle, ', a')>=1)
	{
		$rootTitle = str_ireplace(', a', '', $rootTitle);
		$rootTitle = 'a '.$rootTitle;
	}

	$titleGoogle = urlencode($rootTitle);
	$authorGoogle = urlencode($authorGoogle);

//construct url
$rooturl = 'https://www.googleapis.com/books/v1/volumes?q';
$fields = 'items(volumeInfo/title),items(volumeInfo/authors),items(volumeInfo/publisher),items(volumeInfo/publishedDate),items(volumeInfo/industryIdentifiers),items(volumeInfo/imageLinks/thumbnail)';
//$fields = 'items';
$key = $googleKey;
$maxResults = 5;
if ($authorGoogle=='')
{$authorString = "";}
else
{$authorString = "&inauthor:$authorGoogle";}

$completeurl = "$rooturl=intitle:$titleGoogle"."$authorString&fields=$fields&maxResults=$maxResults&key=$key";

echo "Original search: <a target='_blank' href='$completeurl'>$completeurl</a></br>";

//construct curl request
$request = curl_build($completeurl);
$request2 = explode('"volumeInfo"', $request);//separate into results

foreach ($request2 as $pos=>$item)
{
//separate into sections
$item = str_ireplace('}', '', $item);//remove end }
$itemAr = explode('{',$item); //split by {
echo "<br/>$pos<br/>";
print_r($itemAr);

}

//print_r($request2);
//export_array($request,'Start');

//strip down text
//$strStart = strpos($request, '{');//first bracket - remove everything before this
//$request = substr($request, $strStart); //remove stuff before {

//remove any element with 3 or less characters
/*
foreach ($request3 as $j => $pieceToRemove)
{
if (strlen(trim($pieceToRemove))<=3)
//remove the piece
{
unset($request3[$j]);
}
}*/

//rework the numbering of the array
//$request3 = array_values($request3);

//find requested information
//$finalTitle = $request3[1];

//use some kind of array search to find the author(s), or at least the first one and the end of the author string to get the year
//$startAuthor = array_search('authors', $request3);//array key where authors starts
//$startDate = array_search('publishedDate', $request3);//array key where date starts

//$startAuthorPos = $startAuthor+1;
//$endAuthorPos = $startDate-1;
//$startDatePos = $startDate+1;

//$finalDate = $request3[$startDatePos];
//$finalDate = date("Y",strtotime($finalDate));//format as just a year

//output information
//title
echo "Title: $finalTitle/";

//authors
$k=$startAuthorPos;//set K to the start value
while ($k <= $endAuthorPos)
{
$finalAuthor = $request3[$k];
echo "Author: $finalAuthor ";
$k++;
}
echo "/";//final delimiter
//year
echo "Year: $finalDate/";
echo "<br/>";

}

?>